﻿using Furion.DependencyInjection;
using ST.Torch.WebApi.Core.Manager;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ST.Torch.WebApi.Core.Manage
{

    public class MemoryCacheContext : ICacheManage, ITransient
    {
        private IMemoryCache _objCache;


        public MemoryCacheContext(IMemoryCache objCache)
        {
            _objCache = objCache;
        }
#nullable enable
        public T? Get<T>(string key)
        {
            return _objCache.Get<T>(key);
        }
        public async Task<T?> GetAsync<T>(string key)
        {
            T? obj = _objCache.Get<T>(key);
            return await Task.FromResult(obj);
        }
#nullable disable
        public T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory)
        {

            return _objCache.GetOrCreate<T>(key, factory);
        }
        public async Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> factory)
        {

            return await _objCache.GetOrCreateAsync<T>(key, factory);
        }

        public bool Set<T>(string key, T t, long second = 0, bool IsSlidingExpiration = true)
        {
            var obj = Get<T>(key);
            if (obj != null)
            {
                Remove(key);
            }

            MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
            if (second > 0)
            {
                DateTimeOffset now = DateTimeOffset.Now.AddSeconds(second); // 获取当前时间并包含时区信息
                if (IsSlidingExpiration)
                {
                    options.SlidingExpiration = TimeSpan.FromSeconds(second);
                }
                options.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(second);
            }

            _objCache.Set<T>(key, t, options);   //绝对过期时间
            return true;
        }
        public async Task<bool> SetAsync<T>(string key, T t, long second = 0, bool IsSlidingExpiration = true)
        {
            var obj = Get<T>(key);
            if (obj != null)
            {
                await RemoveAsync(key);
            }

            MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
            if (second > 0)
            {
                DateTimeOffset now = DateTimeOffset.Now.AddSeconds(second); // 获取当前时间并包含时区信息
                if (IsSlidingExpiration)
                {
                    options.SlidingExpiration = TimeSpan.FromSeconds(second);
                }
                options.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(second);
            }

            _objCache.Set<T>(key, t, options);   //绝对过期时间

            return await Task.FromResult(true);
        }

        public bool Remove(string key)
        {
            _objCache.Remove(key);
            return true;
        }
        public async Task<bool> RemoveAsync(string key)
        {
            _objCache.Remove(key);
            return await Task.FromResult(true); ;
        }
    }

}
